import PlayerCtrl from "../../player/PlayerCtrl";
import { NetDefine } from "../../../gamenet/protobufs/NetDefine";
const ws = require("websocket");
const {ccclass, property} = cc._decorator;

@ccclass
export default class UInfo extends cc.Component {
    @property(cc.EditBox)
    nickEditBox: cc.EditBox = null;

    @property(cc.ToggleContainer)
    toggleContainer: cc.ToggleContainer = null;

    private _sex: number = 0;

    onEnable(){
        this.initInfo();
    }

    initInfo(){
        let playerVO = PlayerCtrl.data.vo;
        let sex = playerVO.sex;
        this.setToggleIndex(sex);

        this.nickEditBox.string = playerVO.nickName;
    }

    /** 会自动调用onToggleClick */
    setToggleIndex(index: number){
        let items = this.toggleContainer.toggleItems;
        items[index].isChecked = true;
    }

    onToggleClick(e: cc.Event, index: string){
        this._sex = Number(index);
    }

    onBtnSubmit(){
        if(this.nickEditBox.string == "") return;
        let body = {
            nick: this.nickEditBox.string,
            usex: this._sex
        }
        cc.log(`修改发送`, body);
        ws.send_cmd(NetDefine.ProtocolDefine.Auth, NetDefine.CmdDefine.Auth.EDIT_UINFO, body);
    }
}
